<template>
  <page-header-wrapper :title="false">
    <a-card title="意向单审核">
      <a-tabs type="card">
        <a-tab-pane :key="1" tab="商标意向"></a-tab-pane>
<!--        <a-tab-pane :key="2" tab="专利意向" disabled></a-tab-pane>-->
      </a-tabs>
      <div class="table-operator">
        <s-table
          bordered
          ref="table"
          size="middle"
          :row-key="(record) => record.id"
          :columns="intentionColumns"
          :data="intentionData"
        >
          <div
            slot="filterDropdown"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-input
              v-ant-ref="c => (searchInput = c)"
              :placeholder="`搜索${column.title}`"
              :value="selectedKeys[0]"
              style="width: 188px; margin-bottom: 8px; display: block;"
              @change="e => setSelectedKeys(e.target.value ? [e.target.value] : [])"
              @pressEnter="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
            />
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
              重置
            </a-button>
          </div>
<!--          <div-->
<!--            slot="filterAdviser"-->
<!--            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"-->
<!--            style="padding: 8px"-->
<!--          >-->
<!--            <a-select-->
<!--              show-search-->
<!--              :filter-option="filterOption"-->
<!--              :placeholder="`选择顾问`"-->
<!--              style="width: 188px; margin-bottom: 8px; display: block;"-->
<!--              :value="selectedKeys[0]"-->
<!--              @change="(value) => setSelectedKeys(value ? [value] : [])"-->
<!--              @pressEnter="() => handleSelect1(selectedKeys, confirm, column.dataIndex)"-->
<!--            >-->
<!--              <a-select-option v-for="(item, index) in adviserOpts" :value="index" :key="index">{{item}}</a-select-option>-->
<!--            </a-select>-->
<!--            <a-button-->
<!--              type="primary"-->
<!--              icon="search"-->
<!--              size="small"-->
<!--              style="width: 90px; margin-right: 8px"-->
<!--              @click="() => handleSelect1(selectedKeys, confirm, column.dataIndex)"-->
<!--            >-->
<!--              搜索-->
<!--            </a-button>-->
<!--            <a-button size="small" style="width: 90px" @click="() => handleReset1(clearFilters, column.dataIndex)">-->
<!--              重置-->
<!--            </a-button>-->
<!--          </div>-->
          <div
            slot="filterCategory"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-select
              :placeholder="`选择${column.title}`"
              style="width: 188px; margin-bottom: 8px; display: block;"
              :value="selectedKeys[0]"
              @change="(value) => setSelectedKeys(value ? [value] : [])"
              @pressEnter="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              <a-select-option v-for="(item, index) in categoryOpts" :value="index" :key="index">{{item}}</a-select-option>
            </a-select>
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
              重置
            </a-button>
          </div>
          <div
            slot="filterRegion"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-select
              :placeholder="`选择${column.title}`"
              style="width: 188px; margin-bottom: 8px; display: block;"
              :value="selectedKeys[0]"
              @change="(value) => setSelectedKeys(value ? [value] : [])"
              @pressEnter="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              <a-select-option v-for="(item, index) in regionOpts" :value="index" :key="index">{{item}}</a-select-option>
            </a-select>
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
              重置
            </a-button>
          </div>
          <div
            slot="filterStatus"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-select
              :placeholder="`选择${column.title}`"
              style="width: 188px; margin-bottom: 8px; display: block;"
              :value="selectedKeys[0]"
              @change="(value) => setSelectedKeys(value ? [value] : [])"
              @pressEnter="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              <a-select-option v-for="(item, index) in statusOpts" :value="index" :key="index">{{item}}</a-select-option>
            </a-select>
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
              重置
            </a-button>
          </div>
          <div
            slot="filterRange"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-range-picker
              style="width: 228px; margin-bottom: 8px; display: block;"
              :value="createValue"
              @change="onChange"
            />
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 110px; margin-right: 8px"
              @click="() => handleRange(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 110px" @click="() => rangeReset(clearFilters)">
              重置
            </a-button>
          </div>
          <template slot="username" slot-scope="text, record">
            <div><a-tag>{{text}}</a-tag></div>
            <div v-for="(item, index) in groupOpts" :key="index" style="margin: 5px 0">
              <div v-if="record.group_type == index"><a-tag>{{item}}组</a-tag></div>
            </div>
            <div><a-tag>{{record.leader_name}}</a-tag></div>
          </template>
          <template slot="category" slot-scope="text">第{{text}}类</template>
          <template slot="region" slot-scope="text">
            <div v-for="(item, index) in regionOpts" :key="index">
              <div v-if="text == index">{{item}}</div>
            </div>
          </template>
          <template slot="status" slot-scope="text">
            <a-tag color="red">{{statusOpts[text]}}</a-tag>
<!--            <div v-for="(item, index) in statusOpts" :key="index">-->
<!--              <div v-if="text == index">-->
<!--                <a-button v-if="text == 10" size="small" type="danger" ghost @click="check(record.id)">{{item}}</a-button>-->
<!--                <a-button v-else size="small" type="primary" ghost>{{item}}</a-button>-->
<!--              </div>-->
<!--            </div>-->
          </template>
          <template slot="action" slot-scope="text, record">
            <a-button type="primary" size="small" @click="toDetail(record.id)">查看</a-button>
          </template>
        </s-table>
      </div>
    </a-card>
    <a-modal
      centered
      title="意向单审核"
      v-model="visible"
      @ok="onSubmit"
    >
      <a-form-model ref="form" :model="form">
        <a-form-model-item label="意向审核">
          <a-radio-group v-model="form.status">
            <a-radio v-for="(item, index) in checkOpts" :value="index" :key="index">{{item}}</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item v-if="form.status == 22" label="审核不通过备注">
          <a-input type="textarea" v-model="form.notes" placeholder="注：如审核不通过请填写原因"/>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
  import { STable, Ellipsis } from '@/components'
  import transactionColumns from '@/columns/transaction'
  import { getIntentList, getCheckIntent, postCheckIntent } from '@/api/transaction'

  export default {
    name: 'Intention',
    components: { STable, Ellipsis },
    data () {
      return {
        visible: false,
        form: {},
        queryParam: {},
        createValue: [],
        intent_id: '',
        groupOpts: {},
        regionOpts: {},
        statusOpts: {},
        adviserOpts: {},
        categoryOpts: {},
        checkOpts: {},
        intentionColumns: transactionColumns.intention,
        intentionData: parameters => {
          return getIntentList(Object.assign(parameters, this.queryParam)).then(res => {
            if (res.status === 0) {
              this.groupOpts = res.result.group_type
              this.regionOpts = res.result.tm_region_opts
              this.statusOpts = res.result.statusOpts
              this.adviserOpts = res.result.advisers
              this.categoryOpts = res.result.category_opts
              return res.result
            }
          })
        }
      }
    },
    methods: {
      check (id) {
        const app = this
        getCheckIntent(id).then(res => {
          if (res.status === 0) {
            app.intent_id = id
            app.checkOpts = res.result.status_items
            app.visible = true
          }
        })
      },
      onSubmit () {
        const app = this
        const data = {}
        for (const k in app.form) {
          data['intent[' + k + ']'] = app.form[k]
        }
        postCheckIntent(app.intent_id, data).then(res => {
          if (res.status === 0) {
            app.form = {}
            app.visible = false
            app.$refs.table.refresh(true)
          } else {
            app.$warning({
              title: res.msg
            })
          }
        })
      },
      toDetail (id) {
        this.$router.push({ name: 'IntentDetail', params: { id } })
      },
      handleSelect (selectedKeys, confirm, dataIndex) {
        confirm()
        this.searchText = selectedKeys[0]
        this.searchedColumn = dataIndex
        this.queryParam['filter[' + dataIndex + ']'] = selectedKeys[0]
      },
      handleSelect1 (selectedKeys, confirm, dataIndex) {
        confirm()
        this.searchText = selectedKeys[0]
        this.searchedColumn = dataIndex
        this.queryParam['filter[uid]'] = selectedKeys[0]
      },
      handleReset1 (clearFilters, dataIndex) {
        clearFilters()
        delete this.queryParam['filter[' + dataIndex + ']']
        delete this.queryParam['filter[uid]']
      },
      filterOption (input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        )
      },
      handleSearch (selectedKeys, confirm, dataIndex) {
        confirm()
        this.searchText = selectedKeys[0]
        this.searchedColumn = dataIndex
        this.queryParam['filter[' + dataIndex + ']'] = selectedKeys[0]
      },
      handleReset (clearFilters, dataIndex) {
        clearFilters()
        delete this.queryParam['filter[' + dataIndex + ']']
      },
      onChange (date, dateString) {
        this.createValue = date
        this.queryParam['filter[created_at_start]'] = dateString[0]
        this.queryParam['filter[created_at_end]'] = dateString[1]
      },
      handleRange (selectedKeys, confirm, dataIndex) {
        confirm()
        this.searchText = selectedKeys[0]
        this.searchedColumn = dataIndex
        this.$refs.table.refresh(true)
      },
      rangeReset (clearFilters) {
        clearFilters()
        this.createValue = []
        delete this.queryParam['filter[created_at_start]']
        delete this.queryParam['filter[created_at_end]']
        this.$refs.table.refresh(true)
      }
    }
  }
</script>

<style scoped>

</style>
